package cn.consult.sys.service.impl;

import cn.consult.common.utils.StringUtils;
import cn.consult.netty.SendInstructsService;
import cn.consult.netty.SendInstructsServiceImpl;
import cn.consult.sys.dao.TERuleDao;
import cn.consult.sys.dao.TWeEquipmentDao;
import cn.consult.sys.entity.TERule;
import cn.consult.sys.entity.TWeEquipment;
import cn.consult.sys.service.TERuleService;
import cn.consult.sys.vo.RuleVO;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 电表规则(TERule)表服务实现类
 *
 * @author makejava
 * @since 2024-04-13 11:35:18
 */
@Service("tERuleService")
public class TERuleServiceImpl extends ServiceImpl<TERuleDao, TERule> implements TERuleService {

    @Override
    public IPage<TERule> selectPage(Map<String, Object> params) {
        LambdaQueryWrapper<TERule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(params.get("ruleEnergyMeter") != null && params.get("ruleEnergyMeter") != "", TERule::getRuleEnergyMeter, params.get("ruleEnergyMeter"));


        Page<TERule> rulePage = new Page<>();
        rulePage.setCurrent(Long.valueOf(params.get("pageNum").toString()));
        rulePage.setSize(Long.valueOf(params.get("pageSize").toString()));


        Page<TERule> page = this.baseMapper.selectPage(rulePage, wrapper);
        return page;
    }

    @Autowired
    TWeEquipmentDao tWeEquipmentDao;

    @Autowired
    SendInstructsServiceImpl sendInstructsService;

    @Override
    public boolean saveRule(TERule teRule) throws Exception {

        int insert = this.baseMapper.insert(teRule);

        LambdaQueryWrapper<TWeEquipment> equipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        equipmentLambdaQueryWrapper.eq(TWeEquipment::getEquipmentName, teRule.getRuleEnergyMeter());

        TWeEquipment tWeEquipment = tWeEquipmentDao.selectList(equipmentLambdaQueryWrapper).get(0);

        teRule.setId(tWeEquipment.getId());

        RuleVO ruleVO = new RuleVO();
        BeanUtils.copyProperties(teRule, ruleVO);
        sendInstructsService.sendIotMeterconfigInstructs(ruleVO, null);
        return true;
    }

    @Override
    public boolean updateR(TERule teRule) throws Exception {

        int update = this.baseMapper.updateById(teRule);
        LambdaQueryWrapper<TWeEquipment> equipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        equipmentLambdaQueryWrapper.eq(TWeEquipment::getEquipmentName, teRule.getRuleEnergyMeter());

        TWeEquipment tWeEquipment = tWeEquipmentDao.selectList(equipmentLambdaQueryWrapper).get(0);

        teRule.setId(tWeEquipment.getId());

        RuleVO ruleVO = new RuleVO();
        BeanUtils.copyProperties(teRule, ruleVO);
        sendInstructsService.sendIotMeterconfigInstructs(ruleVO, null);

        return false;
    }
}

